 package client.inventory;

 import com.alibaba.druid.pool.DruidPooledConnection;
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import database.DatabaseConnection;
 import java.awt.Point;
 import java.io.Serializable;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.List;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import server.MapleItemInformationProvider;
 import server.StructAndroid;
 import server.movement.LifeMovement;
 import server.movement.LifeMovementFragment;
 import tools.Randomizer;


 public class MapleAndroid
   implements Serializable
 {
/*  25 */   private static final Logger log = LogManager.getLogger(MapleAndroid.class);
   private static final long serialVersionUID = 9179541993413738569L;
   private final int uniqueid;
   private final int itemid;
/*  29 */   private int Fh = 0;
/*  30 */   private int stance = 0;
   private int skin;
   private int hair;
   private int face;
   private int gender;
   private int type;
   private String name;
/*  37 */   private Point pos = new Point(0, 0);
   private boolean changed = false;

   @JsonCreator
   private MapleAndroid(@JsonProperty("itemid") int itemid, @JsonProperty("uniqueid") int uniqueid) {
/*  42 */     this.itemid = itemid;
/*  43 */     this.uniqueid = uniqueid;
   }




   public static MapleAndroid loadFromDb(int itemid, int uniqueid) {
/*  50 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  51 */       MapleAndroid ret = new MapleAndroid(itemid, uniqueid);
/*  52 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM androids WHERE uniqueid = ?");
/*  53 */       ps.setInt(1, uniqueid);
/*  54 */       ResultSet rs = ps.executeQuery();
/*  55 */       if (!rs.next()) {
/*  56 */         rs.close();
/*  57 */         ps.close();
/*  58 */         return null;
       }
/*  60 */       int type = rs.getInt("type");
/*  61 */       int gender = rs.getInt("gender");
/*  62 */       boolean fix = false;
/*  63 */       if (type < 1) {
/*  64 */         MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/*  65 */         type = ii.getAndroidType(itemid);
/*  66 */         StructAndroid aInfo = ii.getAndroidInfo(type);
/*  67 */         if (aInfo == null) {
/*  68 */           return null;
         }
/*  70 */         gender = aInfo.gender;
/*  71 */         fix = true;
       }
/*  73 */       ret.setType(type);
/*  74 */       ret.setGender(gender);
/*  75 */       ret.setSkin(rs.getInt("skin"));
/*  76 */       ret.setHair(rs.getInt("hair"));
/*  77 */       ret.setFace(rs.getInt("face"));
/*  78 */       ret.setName(rs.getString("name"));
/*  79 */       ret.changed = fix;
/*  80 */       rs.close();
/*  81 */       ps.close();
/*  82 */       return ret;
/*  83 */     } catch (SQLException ex) {
/*  84 */       log.error("加载安卓信息出错", ex);
/*  85 */       return null;
     }
   }





   public static MapleAndroid create(int itemid, int uniqueid) {
/*  94 */     MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/*  95 */     int type = ii.getAndroidType(itemid);
/*  96 */     StructAndroid aInfo = ii.getAndroidInfo(type);
/*  97 */     if (aInfo == null) {
/*  98 */       return null;
     }
/* 100 */     int gender = aInfo.gender;
/* 101 */     int skin = ((Integer)aInfo.skin.get(Randomizer.nextInt(aInfo.skin.size()))).intValue();
/* 102 */     int hair = ((Integer)aInfo.hair.get(Randomizer.nextInt(aInfo.hair.size()))).intValue();
/* 103 */     int face = ((Integer)aInfo.face.get(Randomizer.nextInt(aInfo.face.size()))).intValue();
/* 104 */     if (uniqueid <= -1) {
/* 105 */       uniqueid = MapleInventoryIdentifier.getInstance();
     }
/* 107 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 108 */       PreparedStatement pse = con.prepareStatement("INSERT INTO androids (uniqueid, type, gender, skin, hair, face, name) VALUES (?, ?, ?, ?, ?, ?, ?)");
/* 109 */       pse.setInt(1, uniqueid);
/* 110 */       pse.setInt(2, type);
/* 111 */       pse.setInt(3, gender);
/* 112 */       pse.setInt(4, skin);
/* 113 */       pse.setInt(5, hair);
/* 114 */       pse.setInt(6, face);
/* 115 */       pse.setString(7, "智能机器人");
/* 116 */       pse.executeUpdate();
/* 117 */       pse.close();
/* 118 */     } catch (SQLException ex) {
/* 119 */       log.error("创建安卓信息出错", ex);
/* 120 */       return null;
     }
/* 122 */     MapleAndroid and = new MapleAndroid(itemid, uniqueid);
/* 123 */     and.setType(type);
/* 124 */     and.setGender(gender);
/* 125 */     and.setSkin(skin);
/* 126 */     and.setHair(hair);
/* 127 */     and.setFace(face);
/* 128 */     and.setName("智能机器人");
/* 129 */     return and;
   }




   public void saveToDb() {
/* 136 */     saveToDb(null);
   }




   public void saveToDb(DruidPooledConnection con) {
/* 143 */     if (!this.changed) {
       return;
     }
/* 146 */     boolean needclose = false;
     try {
/* 148 */       if (con == null) {
/* 149 */         needclose = true;
/* 150 */         con = DatabaseConnection.getInstance().getConnection();
       }
/* 152 */       PreparedStatement ps = con.prepareStatement("UPDATE androids SET type = ?, gender = ?, skin = ?, hair = ?, face = ?, name = ? WHERE uniqueid = ?");
/* 153 */       ps.setInt(1, this.type);
/* 154 */       ps.setInt(2, this.gender);
/* 155 */       ps.setInt(3, this.skin);
/* 156 */       ps.setInt(4, this.hair);
/* 157 */       ps.setInt(5, this.face);
/* 158 */       ps.setString(6, this.name);
/* 159 */       ps.setInt(7, this.uniqueid);
/* 160 */       ps.executeUpdate();
/* 161 */       ps.close();
/* 162 */       this.changed = false;
/* 163 */     } catch (SQLException ex) {
/* 164 */       log.error("保存安卓信息出错", ex);
     } finally {
/* 166 */       if (needclose) {
         try {
/* 168 */           con.close();
/* 169 */         } catch (SQLException e) {
/* 170 */           log.error("保存安卓信息出错", e);
         }
       }
     }
   }

   public int getItemId() {
/* 177 */     return this.itemid;
   }

   public int getUniqueId() {
/* 181 */     return this.uniqueid;
   }

   public String getName() {
/* 185 */     return this.name;
   }

   public void setName(String n) {
/* 189 */     this.name = n;
/* 190 */     this.changed = true;
   }

   public int getType() {
/* 194 */     return this.type;
   }

   public void setType(int t) {
/* 198 */     this.type = t;
/* 199 */     this.changed = true;
   }

   public int getGender() {
/* 203 */     return this.gender;
   }

   public void setGender(int g) {
/* 207 */     this.gender = g;
/* 208 */     this.changed = true;
   }

   public int getSkin() {
/* 212 */     return this.skin;
   }

   public void setSkin(int s) {
/* 216 */     this.skin = s;
/* 217 */     this.changed = true;
   }

   public int getHair() {
/* 221 */     return this.hair;
   }

   public void setHair(int h) {
/* 225 */     this.hair = h;
/* 226 */     this.changed = true;
   }

   public int getFace() {
/* 230 */     return this.face;
   }

   public void setFace(int f) {
/* 234 */     this.face = f;
/* 235 */     this.changed = true;
   }




   public int getFh() {
/* 242 */     return this.Fh;
   }

   public void setFh(int Fh) {
/* 246 */     this.Fh = Fh;
   }

   public Point getPos() {
/* 250 */     return this.pos;
   }

   public void setPos(Point pos) {
/* 254 */     this.pos = pos;
   }

   public int getStance() {
/* 258 */     return this.stance;
   }

   public void setStance(int stance) {
/* 262 */     this.stance = stance;
   }

   public void updatePosition(List<LifeMovementFragment> movement) {
/* 266 */     for (LifeMovementFragment move : movement) {
/* 267 */       if (move instanceof LifeMovement) {
/* 268 */         if (move instanceof server.movement.AbsoluteLifeMovement) {
/* 269 */           setPos(move.getPosition());
         }
/* 271 */         setStance(((LifeMovement)move).getNewstate());
       }
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\client\inventory\MapleAndroid.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */